<template>
  <div class="goods-hot">
    <h3>{{ titleObj[props.type] }}</h3>
    <div class="goods-list">
      <!-- 商品区块 -->
      <!-- 这里面将来拿到数据之后，渲染商品列表 -->
      <!-- <GoodsItem v-for="goods in list" :key="goods.id" /> -->
      <GoodsItem v-for="goods in list" :key="goods.id" :goods="goods" />
    </div>
  </div>
</template>

<script lang="ts" setup>
import { PropType, ref, onMounted } from "vue";
import GoodsItem from "@/views/category/components/GoodsItem.vue";

import { ApiRes } from "@/types/data";
import { Goods } from "@/types/category";
import request from "@/utils/request";
import { useRoute } from "vue-router";
const route = useRoute();

// js写法，可以对类型做更加具体的约束
const props = defineProps({
  type: {
    type: Number as PropType<1 | 2 | 3>,
    default: 1,
  },
});

// 标题对象
const titleObj = {
  1: "24小时热销榜",
  2: "周热销榜",
  3: "总热销榜",
};

// 根据商品id和不同的类别发请求，获取对应的商品列表
const list = ref<Goods[]>([]);
const id = route.params.id as string;
const getGoodsList = async () => {
  const res = await request.get<ApiRes<Goods[]>>("/goods/hot", {
    params: { id, type: props.type },
  });

  list.value = res.data.result;
};

onMounted(() => {
  getGoodsList();
});
</script>

<style scoped lang="less">
.goods-hot {
  background-color: #fff;
  margin-bottom: 20px;
  h3 {
    height: 70px;
    background: @helpColor;
    color: #fff;
    font-size: 18px;
    line-height: 70px;
    padding-left: 25px;
    margin-bottom: 10px;
    font-weight: normal;
  }
  .goods-list {
    display: flex;
    flex-direction: column;
    align-items: center;
  }
}
</style>

<!-- <script lang="ts" setup>
import { ref, PropType, onMounted } from "vue";
import GoodsItem from "@/views/category/components/GoodsItem.vue";
import { ApiRes } from "@/types/data";
import { Goods } from "@/types/category";
import request from "@/utils/request";
import { useRoute } from "vue-router";
const route = useRoute();
// js这种写法，可以对我们的类型做更加具体的约束
const props = defineProps({
  type: {
    type: Number as PropType<1 | 2 | 3>,
    default: 1,
  },
});

// 标题对象
const titleObj = {
  1: "24小时热销榜",
  2: "周热销榜",
  3: "总热销榜",
};

// 根据商品id和不同的类别发请求，获取对应的商品列表
const list = ref<Goods[]>([]);
const id = route.params.id as string;
const getGoodsList = async () => {
  const res = await request.get<ApiRes<Goods[]>>("/goods/hot", {
    params: { id, type: props.type },
  });

  list.value = res.data.result;
};

onMounted(() => {
  getGoodsList();
});
</script> -->

<!-- <template>
  <div class="goods-hot">
    <h3>{{ titleObj[props.type] }}</h3>
    <div class="goods-list"> -->
<!-- 渲染商品列表 -->
<!-- <GoodsItem v-for="goods in list" :key="goods.id" :goods="goods" />
    </div>
  </div>
</template> -->
